Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Questions  >  Consider the following C program:#includeint ... Start Learning for Free
Consider the following C program:
#include
int fun(char *s, char *t)
{
for(; *s == *t; s++, t++)
if(*s == '\0')
return 0;
return *s - *t;
 
}
 
int main()
{
char a[10]="Hello";
char b[10]="Help";
char *p = a;
char *q = b;
int x=fun(p,q);
printf("%d", x);
return 0;
}
What will be the output of the program?
    Correct answer is '-4'. Can you explain this answer?
    Verified Answer
    Consider the following C program:#includeint fun(char *s, char *t){for...
    The above C program is used to compare two strings and return an integer. If the first character of two strings are equal, next character of two strings are compared. This continues until the corresponding characters of two strings are different or a null character '\0' is reached.
    In the given C program, the first unmatched character between the two strings a and b is the fourth character. The ASCII value of 'l' is 108 and that of 'p' is 112.
    Therefore, the output will be 108-112 = -4.
    View all questions of this test
    Most Upvoted Answer
    Consider the following C program:#includeint fun(char *s, char *t){for...
    Program Overview
    The provided C program compares two strings, "Hello" and "Help", using a custom function `fun`. Let's analyze how it works step-by-step.
    Function Explanation
    - The `fun` function takes two string pointers `s` and `t`.
    - It iterates through both strings simultaneously comparing their characters.
    - The loop continues as long as the characters pointed to by `s` and `t` are equal (`*s == *t`).
    - If the end of either string is reached (i.e., when `*s` or `*t` is `\0`), it returns 0, indicating the strings are identical up to that point.
    Character Comparison
    - In this case, the first characters are 'H' for both strings, so it continues.
    - The second characters are 'e' in "Hello" and 'e' in "Help", which are equal, continuing the loop.
    - The third characters are 'l' in "Hello" and 'l' in "Help", still equal, and the loop continues.
    - The fourth characters are 'l' in "Hello" and 'p' in "Help", which are different.
    Return Value Calculation
    - At this point, the function exits the loop and computes `*s - *t`, where `*s` is 'l' (ASCII value 108) and `*t` is 'p' (ASCII value 112).
    - The calculation `108 - 112` results in `-4`.
    Output of the Program
    - The value `-4` is stored in `x` and then printed.
    - Therefore, the final output of the program is `-4`.
    This detailed explanation clarifies why the output of the program is `-4`.
    Explore Courses for Computer Science Engineering (CSE) exam
    Question Description
    Consider the following C program:#includeint fun(char *s, char *t){for(; *s == *t; s++, t++)if(*s == '\0')return 0;return *s - *t;}int main(){char a[10]="Hello";char b[10]="Help";char *p = a;char *q = b;int x=fun(p,q);printf("%d", x);return 0;}What will be the output of the program?Correct answer is '-4'. Can you explain this answer? for Computer Science Engineering (CSE) 2025 is part of Computer Science Engineering (CSE) preparation. The Question and answers have been prepared according to the Computer Science Engineering (CSE) exam syllabus. Information about Consider the following C program:#includeint fun(char *s, char *t){for(; *s == *t; s++, t++)if(*s == '\0')return 0;return *s - *t;}int main(){char a[10]="Hello";char b[10]="Help";char *p = a;char *q = b;int x=fun(p,q);printf("%d", x);return 0;}What will be the output of the program?Correct answer is '-4'. Can you explain this answer? covers all topics & solutions for Computer Science Engineering (CSE) 2025 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for Consider the following C program:#includeint fun(char *s, char *t){for(; *s == *t; s++, t++)if(*s == '\0')return 0;return *s - *t;}int main(){char a[10]="Hello";char b[10]="Help";char *p = a;char *q = b;int x=fun(p,q);printf("%d", x);return 0;}What will be the output of the program?Correct answer is '-4'. Can you explain this answer?.
    Solutions for Consider the following C program:#includeint fun(char *s, char *t){for(; *s == *t; s++, t++)if(*s == '\0')return 0;return *s - *t;}int main(){char a[10]="Hello";char b[10]="Help";char *p = a;char *q = b;int x=fun(p,q);printf("%d", x);return 0;}What will be the output of the program?Correct answer is '-4'. Can you explain this answer? in English & in Hindi are available as part of our courses for Computer Science Engineering (CSE). Download more important topics, notes, lectures and mock test series for Computer Science Engineering (CSE) Exam by signing up for free.
    Here you can find the meaning of Consider the following C program:#includeint fun(char *s, char *t){for(; *s == *t; s++, t++)if(*s == '\0')return 0;return *s - *t;}int main(){char a[10]="Hello";char b[10]="Help";char *p = a;char *q = b;int x=fun(p,q);printf("%d", x);return 0;}What will be the output of the program?Correct answer is '-4'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of Consider the following C program:#includeint fun(char *s, char *t){for(; *s == *t; s++, t++)if(*s == '\0')return 0;return *s - *t;}int main(){char a[10]="Hello";char b[10]="Help";char *p = a;char *q = b;int x=fun(p,q);printf("%d", x);return 0;}What will be the output of the program?Correct answer is '-4'. Can you explain this answer?, a detailed solution for Consider the following C program:#includeint fun(char *s, char *t){for(; *s == *t; s++, t++)if(*s == '\0')return 0;return *s - *t;}int main(){char a[10]="Hello";char b[10]="Help";char *p = a;char *q = b;int x=fun(p,q);printf("%d", x);return 0;}What will be the output of the program?Correct answer is '-4'. Can you explain this answer? has been provided alongside types of Consider the following C program:#includeint fun(char *s, char *t){for(; *s == *t; s++, t++)if(*s == '\0')return 0;return *s - *t;}int main(){char a[10]="Hello";char b[10]="Help";char *p = a;char *q = b;int x=fun(p,q);printf("%d", x);return 0;}What will be the output of the program?Correct answer is '-4'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice Consider the following C program:#includeint fun(char *s, char *t){for(; *s == *t; s++, t++)if(*s == '\0')return 0;return *s - *t;}int main(){char a[10]="Hello";char b[10]="Help";char *p = a;char *q = b;int x=fun(p,q);printf("%d", x);return 0;}What will be the output of the program?Correct answer is '-4'. Can you explain this answer? tests, examples and also practice Computer Science Engineering (CSE) tests.
    Explore Courses for Computer Science Engineering (CSE) exam
    Signup to solve all Doubts
    Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
    10M+ students study on EduRev